﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace iMusik.Mp3ZingOnline
{
    public class ZingTopObject
    {
        private string _MainUrl;

        private List<Mp3Song> _TopListSong;
        private List<Mp3Video> _TopListVideo;
        private List<Mp3Album> _TopListAlbum;
        private bool _IsSetSong;
        private bool _IsSetAlbum;
        private bool _IsSetVideo;

        public List<Mp3Video> TopListVideo
        {
            get { return _TopListVideo; }
            private set { _TopListVideo = value; }
        }
        public List<Mp3Song> TopListSong
        {
            get { return _TopListSong; }
            private set { _TopListSong = value; }
        }
        public List<Mp3Album> TopListAlbum
        {
            get { return _TopListAlbum; }
            private set { _TopListAlbum = value; }
        }

        public ZingTopObject(string url)
        {
            _MainUrl = url;
            _TopListAlbum = new List<Mp3Album>();
            _TopListVideo = new List<Mp3Video>();
            _TopListSong = new List<Mp3Song>();
            _IsSetSong = false;
            _IsSetAlbum = false;
            _IsSetVideo = false;
        }

        public async Task SetAllInformation()
        {
            
            try
            {
                string source = await Mp3Zing.DownloadString(new Uri(_MainUrl));
                int i = 0;
                while (i < source.Length)
                {
                    if (source[i] == 34)
                    {
                        source = source.Remove(i, 1);
                        i--;
                    }
                    i++;
                }

                string pattern = @"(?<=href=/bang-xep-hang).*?(html)";
                Regex regex = new Regex(pattern);
                MatchCollection mc = regex.Matches(source);
                string linkAlbum = mc[1].Value;
                linkAlbum = "http://m.mp3.zing.vn/bang-xep-hang" + linkAlbum;
                string linkVideo = mc[2].Value;
                linkVideo = "http://m.mp3.zing.vn/bang-xep-hang" + linkVideo;

                if (_TopListSong.Count == 0)
                    SetListSong(source);
                if (_TopListAlbum.Count == 0)
                    await SetListAlbum(linkAlbum);
                if (_TopListVideo.Count == 0)
                    await SetListVideo(linkVideo);
                
            }
            catch (Exception ex)
            {
                throw (ex);
            }

        }

        private void SetListSong(string source)
        {
            if (_IsSetSong)
                return;
            try
            {
                int i = 0;
                while (i < source.Length)
                {
                    if (source[i] == 34)
                    {
                        source = source.Remove(i, 1);
                        i--;
                    }
                    i++;
                }

                List<string> listSongName = GetListName(source);
                List<string> listSingerName = GetListSingerName(source);
                List<string> listSongLocalLink = GetListLocaLink(source);

                i = 0;
                foreach (string link in listSongLocalLink)
                {
                    _TopListSong.Add(new Mp3Song(listSongName[i], listSingerName[i], link, true));
                    i++;
                }
                _IsSetSong = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }

        }

        private async Task SetListVideo(string url)
        {
            if (_IsSetVideo)
                return;
            try
            {
                string source = await Mp3Zing.DownloadString(new Uri(url));
                int i = 0;
                while (i < source.Length)
                {
                    if (source[i] == 34)
                    {
                        source = source.Remove(i, 1);
                        i--;
                    }
                    i++;
                }

                List<string> listVideoName = GetListName(source);
                List<string> listSingerName = GetListSingerName(source);
                List<string> listVideoLocalLink = GetListLocaLinkVideo(source);
                List<string> listImgLink = GetListImgLink(source);

                i = 0;
                foreach (string link in listVideoLocalLink)
                {
                    _TopListVideo.Add(new Mp3Video(listVideoName[i], listSingerName[i], listImgLink[i], link));
                    i++;
                }
                _IsSetVideo = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private async Task SetListAlbum(string url)
        {
            if (_IsSetAlbum)
                return;
            try
            {
                string source = await Mp3Zing.DownloadString(new Uri(url));
                int i = 0;
                while (i < source.Length)
                {
                    if (source[i] == 34 || source[i] == '\n')
                    {
                        source = source.Remove(i, 1);
                        i--;
                    }
                    i++;
                }

                List<string> listAlbumName = GetListName(source);
                List<string> listSingerName = GetListSingerName(source);
                List<string> listAlbumLocalLink = GetListLocaLinkAlbum(source);
                List<string> listImgLink = GetListImgLink(source);

                i = 0;
                foreach (string link in listAlbumLocalLink)
                {
                    if (i > (listImgLink.Count - 1))
                        listImgLink.Add("cai nay la gif");
                    _TopListAlbum.Add(new Mp3Album(listAlbumName[i], listSingerName[i], listImgLink[i], link));
                    i++;
                }
                _IsSetAlbum = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }

        }

        private List<string> GetListSingerName(string source)
        {
            List<string> listSingerName = new List<string>();
            string pattern = @"(?<=<h4>).*?(?=<)";
            Regex regex3 = new Regex(pattern);
            MatchCollection mc3 = regex3.Matches(source);
            foreach (Match m1 in mc3)
            {
                string temp2 = m1.Value;
                temp2 = Mp3Zing.ConvertStringAsciiToUtf8(temp2);
                listSingerName.Add(temp2);

            }
            return listSingerName;
        }

        private List<string> GetListName(string source)
        {
            List<string> listSongAlbumName = new List<string>();
            string pattern = @"(?<=<h3>).*?(?=<)";
            Regex regex2 = new Regex(pattern);
            MatchCollection mc2 = regex2.Matches(source);
            foreach (Match m1 in mc2)
            {
                string temp2 = m1.Value;
                temp2 = Mp3Zing.ConvertStringAsciiToUtf8(temp2);
                listSongAlbumName.Add(temp2);

            }
            return listSongAlbumName;
        }

        private List<string> GetListLocaLink(string source)
        {
            List<string> listSongLocalLink = new List<string>();
            string pattern = @"(?<=href=/bai-hat).*?(html)";
            Regex regex1 = new Regex(pattern);
            MatchCollection mc1 = regex1.Matches(source);
            int j = 0;
            foreach (Match m1 in mc1)
            {
                string temp = "http://m.mp3.zing.vn/bai-hat";
                temp += m1.Value;
                listSongLocalLink.Add(temp);
            }
            return listSongLocalLink;
        }

        private List<string> GetListLocaLinkVideo(string source)
        {
            List<string> listSongLocalLink = new List<string>();
            string pattern = @"(?<=href=/video-clip).*?(html)";
            Regex regex1 = new Regex(pattern);
            MatchCollection mc1 = regex1.Matches(source);
            int j = 0;
            foreach (Match m1 in mc1)
            {
                string temp = "http://m.mp3.zing.vn/video-clip";
                temp += m1.Value;
                listSongLocalLink.Add(temp);
            }
            return listSongLocalLink;
        }

        private List<string> GetListImgLink(string source)
        {
            List<string> listLink = new List<string>();
            string pattern = @"(?=http://image).*?(jpg|png)";
            Regex regex = new Regex(pattern);
            MatchCollection mc = regex.Matches(source);
            int j;
            foreach (Match m1 in mc)
            {
                listLink.Add(m1.Value);
            }
            return listLink;
        }

        private List<string> GetListLocaLinkAlbum(string source)
        {
            List<string> listSongLocalLink = new List<string>();
            string pattern = @"(?<=href=/album).*?(html)";
            Regex regex1 = new Regex(pattern);
            MatchCollection mc1 = regex1.Matches(source);
            int j = 0;
            foreach (Match m1 in mc1)
            {
                string temp = "http://m.mp3.zing.vn/album";
                temp += m1.Value;
                listSongLocalLink.Add(temp);
            }
            return listSongLocalLink;
        }

    }
}
